package cn.xorange.commons.file.controller;

import cn.xorange.commons.file.configuration.FileConfig;
import cn.xorange.commons.file.entity.FileBean;
import cn.xorange.commons.file.entity.FileCode;
import cn.xorange.commons.file.exception.UploadException;
import cn.xorange.commons.file.service.LocalUploadService;
import cn.xorange.commons.file.service.QiNiuUploadService;
import cn.xorange.commons.file.utils.FileUtil;
import cn.xorange.commons.utils.io.FileUtils;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.util.Objects;

@RestController
@RequestMapping("/file")
public class UploadController {

    @Resource
    FileConfig config;
    @Resource
    QiNiuUploadService qiNiuUploadService;
    @Resource
    LocalUploadService localUploadService;


    @PostMapping("/upload")
    public JSONObject upload(@RequestParam(value = "file") MultipartFile file,
                           @RequestParam(value = "oss", required = false) String oss,
                           @RequestParam(value = "path", required = false) String path) throws UploadException {
        if(Objects.isNull(path)){
            throw UploadException.filePathNull();
        }

        String ossName = config.getOssName(oss);
        String[] suffix = FileUtil.getUploadSuffix(path);
        FileCode code = FileUtil.checkFileNameAndExtend(file,suffix);
        if(Objects.nonNull(code)){
            throw UploadException.exception(code);
        }
        code = FileUtil.checkFileSize(file,suffix);
        if(Objects.nonNull(code)){
            throw UploadException.exception(code);
        }

        String filePath = "";
        File sourceFile = FileUtils.multipartFileToFile(file);
        if(ossName.equals(config.qiniuProperties().getName())){
            filePath = this.qiNiuUploadService.uploadFile(path, sourceFile);
        }else {
            // 本地存储
            filePath = this.localUploadService.uploadFile(path, sourceFile);
        }
        sourceFile.delete();
        return FileBean.onSuccess(filePath);
    }

    @PostMapping("/delete")
    public FileBean delete(@RequestBody JSONObject obj) throws UploadException {
        String path = obj.getString("path");
        if(Objects.isNull(path) || path.isEmpty()){
            throw UploadException.filePathNull();
        }
        if (path.startsWith(config.localProperties().getBucketName())) {
            this.localUploadService.delFile(path);
            return FileBean.onSuccess();
        } else if(path.startsWith(config.qiniuProperties().getBucketName())) {
            String key = path.replace(this.config.qiniuProperties().getBucketName()+"/", "");
            this.qiNiuUploadService.delFile(key);
            return FileBean.onSuccess();
        }else {
            throw UploadException.fileOssNotFound();
        }
    }



    @PostMapping("/url")
    public FileBean getUrl(@RequestBody JSONObject obj) throws UploadException {
        String path = obj.getString("path");
        String url = "";
        if (path.startsWith(config.localProperties().getBucketName())) {
            url = this.localUploadService.getUrl(path);
            return FileBean.onSuccess(url);
        } else if(path.startsWith(config.qiniuProperties().getBucketName())) {
            url = this.qiNiuUploadService.getUrl(path);
            return FileBean.onSuccess(url);
        } else {
            throw UploadException.fileOssNotFound();
        }
    }

}
